﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;

namespace WCFServiceWebRole1
{
    public class UserDataServiceContext : TableServiceContext
    {
        private static String TableName = "Users";

        public IQueryable<User> Users
        {
            get
            {
                return this.CreateQuery<User>(TableName);
            }
        }

        public UserDataServiceContext(string baseAddress, StorageCredentials credentials)
            : base(baseAddress, credentials)
        {
            this.ResolveType = x => typeof(User);
        }

       /* public double getUser(string userId)
        {
            return (from u in Users
                    where
                    u.RowKey == userId
                    select u.Latitude).First();
        }*/

        public int maxUserId()
        {
            return (from u in Users
                    select u).ToList().Max(u => Convert.ToInt32(u.RowKey));
        }

        public User loginUser(string email, string password)
        {
            return (from u in Users
                    where
                    u.Email.Equals(email) && u.Password.Equals(password)
                    select u).FirstOrDefault();
        }

        public bool registerUser(User newUser)
        {
            var existCheck = (from u in Users
                              where u.Email.Equals(newUser.Email)
                              select u).FirstOrDefault();

            if (existCheck == null)
            { // only can register user if the email doesn't already exist
                this.AddObject("Users", newUser);
                this.SaveChanges();

                return true;
            }
            return false;
        }

        public List<User> getUsers(List<string> userIds)
        {
            return (from u in Users
                    select u).ToList().Select(f => f).Where(f => userIds.Contains(f.RowKey)).ToList();

         }

        public void createTestUsers()
        {
            User one = new User("1", "Justin", "j1@gmail.com", "default", new DateTime(1980, 2, 3));
            User two = new User("2", "Mark",  "m1@gmail.com", "default", new DateTime(1982, 2, 3));
            User three = new User("3", "Sid", "s1@gmail.com", "default", new DateTime(1983, 2, 3));
            User four = new User("4", "Suraj", "s2@gmail.com", "default", new DateTime(1984, 2, 3));
            User five = new User("5", "Ron", "r1@gmail.com", "default", new DateTime(1985, 2, 3));

            this.AddObject("Users", one);
            this.AddObject("Users", two);
            this.AddObject("Users", three);
            this.AddObject("Users", four);
            this.AddObject("Users", five);
            this.SaveChanges();
        }


        /*// retrieve scientific job from table
        public ScientificJob GetScientificJob(string rowKey)
        {
            return (from r in this.ScientificJobs
                    where
                    r.RowKey == rowKey
                    select r).First();
        }

        // save changes of scientific job to table
        public void UpdateScientificJob(ScientificJob j)
        {
            this.UpdateObject(j);
            this.SaveChanges();
        }

        // delete scientific job from table
        public void DeleteScientificJob(ScientificJob j)
        {
            this.DeleteObject(j);
            this.SaveChanges();
        }*/
    }
}
